import { update } from '../dom/render'
import { isUndefined } from '../types'

let currentVdom;

export const setCurrentVdom = (value) => {
  currentVdom = value
  return value
}

export function useState(defaultValue) {
  const _innerVdom = currentVdom
  const state = _innerVdom._state
  const currentIndex = _innerVdom._index++
  if (isUndefined(state[currentIndex])) {
    state[currentIndex] = defaultValue
  }
  function setState(value) {
    state[currentIndex] = value
    _innerVdom._index = 0
    update(_innerVdom)
  }

  return [
    state[currentIndex],
    setState
  ]
}
